﻿using System.Windows;
using System.Windows.Controls;
using compshop.Modules.ClientOrderEdit.Presenter;
using System.Collections.Generic;
using compshop.Infrastructure.CompShopDataService;
using System;
using System.Windows.Documents;


namespace compshop.Modules.ClientOrderEdit.Views
{
    /// <summary>
    /// Логика взаимодействия для ClientOrderListView.xaml
    /// </summary>
    public partial class ClientOrderEditView : UserControl, IViewClientOrderEdit
    {
        public ClientOrderEditPresenter Presenter { get; set; }

        public ClientOrderEditView()
        {
            InitializeComponent();
        }

        public ClientOrderEditView(ClientOrderEditPresenter prsnt)
            :this()
        {
            Presenter = prsnt;
            Presenter.View = this;
            UpdateData();
        }

        public void ShowData()
        {
        }

        public void UpdateData()
        {
            DataContext = null;
            DataContext = Presenter;
            switch (Presenter.ClientOrder.Status)
            { 
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                   
                    btnConfirm.Visibility = Visibility.Visible;
                    btnTypeCheck.Visibility = Visibility.Visible;
                    btnPrintBill.Visibility = Visibility.Visible;
                    spCheck.Visibility = Visibility.Hidden;
                    spDate.Visibility = Visibility.Hidden;
                    spManager.Visibility = Visibility.Hidden;
                    break;
                case 6:
                case 7:
                   
                    btnConfirm.Visibility = Visibility.Hidden;
                    btnTypeCheck.Visibility = Visibility.Hidden;
                    btnPrintBill.Visibility = Visibility.Hidden;
                    spCheck.Visibility = Visibility.Visible;
                    spDate.Visibility = Visibility.Visible;
                    spManager.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void btnTypeCheck_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowCheckDialog();
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ConfirmDelivery();
        }

        private void btnPrintBill_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDlg = new PrintDialog();
            if (printDlg.ShowDialog() == true)
            {
                Grid grid = new Grid();

                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(100);
                grid.RowDefinitions.Add(rowdef);
                grid.RowDefinitions.Add(new RowDefinition());

                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = new GridLength(150);
                grid.ColumnDefinitions.Add(coldef);
                grid.ColumnDefinitions.Add(new ColumnDefinition());

                TextBlock header = new TextBlock();
                grid.Children.Add(header);
                Grid.SetRow(header, 0);
                Grid.SetColumn(header, 0);
                Grid.SetColumnSpan(header, 2);

                string data = string.Empty;
                string price = string.Empty;

                data = string.Format("{0}, {1}\nДата: {2}\nМенеджер: {3}\n\nКвитанция № {4}\n\n",
                    Presenter.ClientOrder.Department.Name, Presenter.ClientOrder.Department.Address, DateTime.Now.ToLongDateString(),
                    Presenter.ClientOrder.Employee.LastName, Presenter.ClientOrder.ClientOrderID);
                header.Inlines.Add(new Run(data));
                header.Margin = new Thickness(10);

                data = string.Empty;
                foreach (Product prod in Presenter.ClientOrder.Products)
                {
                    data += string.Format("{0} {1}\n", prod.GeneralProduct.Producer.Name, prod.GeneralProduct.Name);
                    price += string.Format(" {0 :F2}\n", prod.GeneralProduct.CurrentPrice);
                }
                data += "\nИтого: ";
                price += string.Format("\n {0 :F2}", Presenter.Total);

                TextBlock visualLeft = new TextBlock();
                visualLeft.Inlines.Add(new Run(data));
                visualLeft.Margin = new Thickness(10);
                grid.Children.Add(visualLeft);
                Grid.SetRow(visualLeft, 1);
                Grid.SetColumn(visualLeft, 0);

                TextBlock visualRight = new TextBlock();
                visualRight.Inlines.Add(new Run(price));
                visualRight.Margin = new Thickness(10);
                Grid.SetRow(visualRight, 1);
                Grid.SetColumn(visualRight, 1);

                grid.Margin = new Thickness(10);
                grid.Children.Add(visualRight);

                Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);
                grid.Measure(pageSize);
                grid.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
                printDlg.PrintVisual(grid, "PrintCheck");
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            Presenter.RemoveGoods(GetSelectedProducts());
        }

        private void btnReplace_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ReplaceGoods(GetSelectedProducts());
        }

        private IList<Product> GetSelectedProducts()
        {
            IList<Product> list = new List<Product>();
            CheckBox chk;
            foreach (Product p in dgGoods.Items)
            {
                chk = dgGoods.Columns[6].GetCellContent(p) as CheckBox;
                if (chk != null && chk.IsChecked == true)
                    list.Add(p);
            }
            return list;
        }

    }
}
